package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.merchant.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * huhj
 */

@RestController
@RequestMapping("/wardrobe/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    @GetMapping("/getlist")
    public List getList(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return categoryService.getPList(orgId);
    }

    @GetMapping("/getinfo")
    public Map<String, Object> getInfo(@RequestParam(value = "pid") Integer pid,HttpServletRequest request){
        if(null == pid){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Map<String, Object> detail = categoryService.getDetail(pid,orgId);
        List<Map<String, Object>> sList = categoryService.getSList(pid,orgId);
        detail.put("categorySubList",sList);
        return detail;
    }

    @GetMapping("/getallslist")
    public List getAllsList(@RequestParam(value = "pid") Integer pid,HttpServletRequest request){
        if(null == pid){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return categoryService.getAllSList(pid,orgId);
    }

    @PostMapping("/changestatus")
    public boolean changeStatus(@RequestBody Map<String, Integer> map,HttpServletRequest request){
        if(StringUtils.isEmpty(map.get("categoryId"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return categoryService.changeStatus(map.get("categoryId"),orgId);
    }

    @PostMapping("/addsub")
    public Long addSub(@RequestBody Map<String, Object> map,HttpServletRequest request){
        if(StringUtils.isEmpty(map.get("categoryId")) || StringUtils.isEmpty(map.get("subName"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        boolean subNameCount = categoryService.cheackRename(map.get("subName").toString(),orgId);
        if(subNameCount){
            throw GlobalException.build("类目名称已存在");
        }
        map.put("orgId",orgId);
        categoryService.addsCategory(map);
        return (Long) map.get("id");
    }

    @PostMapping("/renamesub")
    public boolean renameSub(@RequestBody Map<String, Object> map,HttpServletRequest request){
        if(StringUtils.isEmpty(map.get("subId")) || StringUtils.isEmpty(map.get("subName"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        boolean subNameCount = categoryService.cheackRename(map.get("subName").toString(),orgId);
        if(subNameCount){
            throw GlobalException.build("类目名称已存在");
        }
        map.put("orgId",orgId);
        return categoryService.renameSub(map);
    }

    @PostMapping("/removesub")
    public boolean removeSub(@RequestBody Map<String, Object> map,HttpServletRequest request){
        if(StringUtils.isEmpty(map.get("categoryId")) || StringUtils.isEmpty(map.get("subId"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        return categoryService.removesCategory(map);
    }

    @GetMapping("/getattributelist")
    public List getAttributeList(@RequestParam(value = "categoryId") Integer categoryId,HttpServletRequest request){
        if(StringUtils.isEmpty(categoryId)){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return categoryService.getAttributeList(categoryId,orgId);
    }

    @GetMapping("/getgoodscateattr")
    public Map getGoodsCateAttr(@RequestParam(value = "goodsId") Long goodsId,HttpServletRequest request){
        if(StringUtils.isEmpty(goodsId)){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return categoryService.getGoodsCateAttr(goodsId,orgId);
    }

}
